import { useEffect, useState, useMemo, useCallback } from "react";
import { arrToObj } from "@/utils";

const useGoodsSelected = (goodsList) => {
  const [goodsSelected, setGoodsSelected] = useState({});
  const [goodsMapObj, setGoodsMapObj] = useState(arrToObj(goodsList));
  const baseGoodsObj = useMemo(() => arrToObj(goodsList), [goodsList]);
  const baseGoodsObjKeys = Object.keys(baseGoodsObj);
  const allItemsArray = baseGoodsObjKeys.reduce((total, next) => {
    return [...total, ...baseGoodsObj[next]];
  }, []);

  const getKeyToLineFun = useCallback(() => {
    const keyToLine = {};
    baseGoodsObjKeys.forEach((baseGoodsObjKey) => {
      // A B C
      baseGoodsObj[baseGoodsObjKey].forEach((goodsOne) => {
        //goodsOne.goodsname
        allItemsArray.forEach((item) => {
          const sitllGoods = goodsList
            .filter((item2) => item2.quantity > 0)
            .filter(
              (item3) =>
                item3.goods[baseGoodsObjKey].goodsname === goodsOne.goodsname &&
                item3.goods[item.parentName].goodsname === item.goodsname
            );
          const goodNums =
            item.parentName === baseGoodsObjKey
              ? true
              : sitllGoods.length
              ? true
              : false;

          if (keyToLine[baseGoodsObjKey]) {
            if (!keyToLine[baseGoodsObjKey][goodsOne.goodsname])
              keyToLine[baseGoodsObjKey][goodsOne.goodsname] = [
                { ...item, goodNums },
              ];
            else
              keyToLine[baseGoodsObjKey][goodsOne.goodsname].push({
                ...item,
                goodNums,
              });
          } else if (!keyToLine[baseGoodsObjKey]) {
            keyToLine[baseGoodsObjKey] = {};
            keyToLine[baseGoodsObjKey][goodsOne.goodsname] = [
              { ...item, goodNums },
            ];
          }
        });
      });
    });
    return keyToLine;
  }, [allItemsArray, baseGoodsObj, baseGoodsObjKeys, goodsList]);

  const getNoSelectArrayFun = useCallback(() => {
    const noSelectArray = [];
    allItemsArray.forEach((myItem) => {
      const goodNums = getKeyToLineFun()
        [myItem.parentName][myItem.goodsname].filter(
          (item2) => item2.parentName !== myItem.parentName
        )
        .reduce((total, next) => {
          return total || next.goodNums;
        }, false);
      noSelectArray.push({
        ...JSON.parse(JSON.stringify(myItem)),
        goodNums,
      });
    });
    return noSelectArray;
  }, [allItemsArray, getKeyToLineFun]);

  const getFinallyDataFun = useCallback(() => {
    const retrunData = [getNoSelectArrayFun()];
    const goodsSelectedKeys = Object.keys(goodsSelected);

    goodsSelectedKeys.forEach((itemKey) =>
      retrunData.push(getKeyToLineFun()[itemKey][goodsSelected[itemKey]])
    );
    return retrunData;
  }, [getKeyToLineFun, getNoSelectArrayFun, goodsSelected]);

  const getResolveFinDataFun = useCallback((finallyData) => {
    if (finallyData.length <= 1) return finallyData[0];
    return finallyData.reduce((total, next) => {
      return total.map((item) => {
        const compareItem = next.find(
          (item2) =>
            item2.parentName === item.parentName &&
            item2.goodsname === item.goodsname
        );
        return { ...item, goodNums: item.goodNums && compareItem.goodNums };
      });
    });
  }, []);

  const getFinBaseGoodsObj = useCallback(
    (resolveFinData) => {
      const copyBaseGoodsObj = JSON.parse(JSON.stringify(baseGoodsObj));
      baseGoodsObjKeys.forEach((baseGoodsObjKey) => {
        copyBaseGoodsObj[baseGoodsObjKey].forEach((goodsOne) => {
          goodsOne.goodNums = resolveFinData.find(
            (item) =>
              item.parentName === goodsOne.parentName &&
              item.goodsname === goodsOne.goodsname
          ).goodNums;
        });
      });
      return copyBaseGoodsObj;
    },
    [baseGoodsObj, baseGoodsObjKeys]
  );

  useEffect(() => {
    setGoodsMapObj(
      getFinBaseGoodsObj(getResolveFinDataFun(getFinallyDataFun()))
    );
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [goodsSelected]);

  return [
    goodsSelected,
    setGoodsSelected,
    goodsMapObj,
    baseGoodsObjKeys,
    setGoodsMapObj,
  ];
};

export default useGoodsSelected;
